(function() {
	'use strict';
	/*===========================
    Slide
    ===========================*/
	var Slide = function(container,params) {

		var _this = document.getElementsByClassName(container)[0],
			slideWrap = _this.getElementsByClassName('slide-wrap')[0],
			slides = slideWrap.getElementsByClassName('slide-item'),
			slidesL = slides.length,
			btn = _this.getElementsByClassName('btn'),
			round = _this.getElementsByClassName('index-round')[0],
			roundis = round.getElementsByTagName('li'),
			btnPrev = btn[0],
			btnNext = btn[1],
			index = 0;
		// 参数配置
		params = params || {};
		var defaults = {
			loop : false,			// 是否支持_this.params.loop播放
			dragSlide : true,		// 是否允许PC端拖拽切换item, 注： 此时的item上的默认事件会被禁止
			autoplay : true,		// 是否允许自动切换
			autoplayId: null,		// 自动切换的定时器名称
			autoplayTime: 5000		// 自动切换的时间间隔
		}
		for(var def in defaults){
			if(typeof params[def] === 'undefined'){
				params[def] = defaults[def]
			}else if (typeof params[def] === 'object') {
                for (var deepDef in defaults[def]) {
                    if (typeof params[def][deepDef] === 'undefined') {
                        params[def][deepDef] = defaults[def][deepDef];
                    }
                }
            }
		}
		_this.params = params;

		slideWrap.style.transition = 'all 0.4s';
		slideWrap.style.width = slidesL + '00%';

		_this.init = function() {
			/* 判断设备,区别显示翻页按钮 */
			var device = browserRedirect();
			if (device.mobile) {
				// console.log('移动端')
				btnPrev.style.display = 'none';
				btnNext.style.display = 'none';
			} else {
				// console.log('PC端')
				btnPrev.style.display = 'block';
				btnNext.style.display = 'block';
			}

			/* 窗口发生变化时 */
			window.onresize = function() {
				var device = browserRedirect()
				if (device.mobile) {
					// console.log('移动端')
					btnPrev.style.display = 'none';
					btnNext.style.display = 'none';
				} else {
					// console.log('PC端')
					btnPrev.style.display = 'block';
					btnNext.style.display = 'block';
				}
			}

			/* 重置每个滑块的宽度,加入索引小圆圈dom并未第一个加上active  */
			var slideWidth = (100 / slidesL).toFixed(2) + '%';

			for (var i = 0; i < slidesL; i++) {
				slides[i].style.width = slideWidth;
				var lis = document.createElement("li");
				lis.setAttribute('index', i);
				round.append(lis);
			}
			round.getElementsByTagName('li')[0].classList.add('active');

			
			if (_this.params.loop) {
				/* 循环轮播时子元素的复制 */
				var childsList = slideWrap.children;
				var firstChildCopy = childsList[0].cloneNode(true);
				var lastChildCopy = childsList[childsList.length - 1].cloneNode(true);
				slideWrap.insertBefore(lastChildCopy, childsList[0]);
				slideWrap.appendChild(firstChildCopy);
				slideWrap.style.left = '-100%';
				index = 1;
			}else{
				slideWrap.style.left = '0';
			}
		}
		_this.init();

		/*===========================
	    判断设备(PC,H5)的方法
	    ===========================*/
		function browserRedirect() {
			var sUserAgent = navigator.userAgent.toLowerCase(),
				bIsIpad = sUserAgent.match(/ipad/i) == "ipad",
				bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os",
				bIsMidp = sUserAgent.match(/midp/i) == "midp",
				bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4",
				bIsUc = sUserAgent.match(/ucweb/i) == "ucweb",
				bIsAndroid = sUserAgent.match(/android/i) == "android",
				bIsCE = sUserAgent.match(/windows ce/i) == "windows ce",
				bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile",
				device = {
					mobile: false,
					pc: false
				};
			if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
				device.mobile = true;
			} else {
				device.pc = true;
			}
			return device;
		}

		/*===========================
	    transition 完成后的回调函数
	    ===========================*/
		_this.tranEndFun = function() {
			if (!_this.params.loop) {
				return;
			}
			if (index > slidesL) {
				slideWrap.style.transition = 'none';
				slideWrap.style.left = '-100%';
				setTimeout(function() {
					slideWrap.style.transition = 'all 0.4s'
				}, 0);
				index = 1;
				return;
			}
			if (index < 1) {
				slideWrap.style.transition = 'none';
				slideWrap.style.left = -1 * slidesL + '00%';
				setTimeout(function() {
					slideWrap.style.transition = 'all 0.4s'
				}, 0);
				index = slidesL;
				return;
			}
		}

		/*===========================
	    slide 函数
	    param: {num} 传入slide-item 的索引,播放哪一个item ,并改变索引圆圈的状态
	    ===========================*/
		_this.slideTo = function(param) {
			slideWrap.style.left = -1 * (param) + '00%';
			for (var i = 0; i < roundis.length; i++) {
				roundis[i].classList.remove('active');
			}
			if(_this.params.loop){
				if(param <= slidesL && param >= 1){
					roundis[param-1].classList.add('active');
				}else if(param == 0){
					roundis[slidesL-1].classList.add('active');
				}else{
					roundis[0].classList.add('active');
				}
			}else{
				roundis[param].classList.add('active');
			}
		}

		/*===========================
	    点击前/后的按钮时，切换item
	    ===========================*/
		_this.slideFuncPrev = function() {
			if(index >= 1){
				index--;
			}
			_this.slideTo(index);
		}
		_this.slideFuncNext = function() {
			if (!_this.params.loop && index < slidesL - 1) {
				index++;
			}else if (_this.params.loop && index <= slidesL) {
				index++;
			}
			_this.slideTo(index);
		}
		if(_this.params.autoplay){
			autoplayFun(_this.params.autoplayTime);
		}
		function autoplayFun (time) {
			_this.params.autoplayId = setInterval(_this.slideFuncNext,time);
		}
		_this.addEventListener('mouseover', function(){
			clearInterval(_this.params.autoplayId)
		});
		_this.addEventListener('mouseout', function(){
			if(_this.params.autoplay){
				autoplayFun(_this.params.autoplayTime);
			}
		});

		btnPrev.addEventListener('click', _this.slideFuncPrev);
		btnNext.addEventListener('click', _this.slideFuncNext);
		slideWrap.addEventListener('transitionend', _this.tranEndFun);

		/*===========================
	    鼠标移入圆圈时，切换item
	    ===========================*/
		for (var i = 0; i < roundis.length; i++) {
			var a = roundis[i];
			a.index = i;
			a.addEventListener('mouseover', function() {
				if(_this.params.loop){
					_this.slideTo(this.index+1);
				} else {
					_this.slideTo(this.index);
				}
			})
		}

		var startX;
		/*===========================
		PC端拖拽时，切换item
		===========================*/
		_this.addEventListener('mousedown',function(e){
			if(_this.params.dragSlide){
				e.preventDefault();
			}
			startX = e.clientX;
		})
		_this.addEventListener('mouseup',function(e){
			var endX = e.clientX,
				X = endX - startX;
			if(X > 10){
				_this.slideFuncPrev();
			}else if(X < -10){
				_this.slideFuncNext();
			}
		})

		/*===========================
		移动端拖拽时，切换item
		===========================*/
		_this.addEventListener('touchstart', function(e) {
			var touch = e.changedTouches[0];
			startX = touch.pageX;
		})
		_this.addEventListener('touchend', function(e) {
			var touch = e.changedTouches[0],
				moveEndX = touch.pageX,
				X = moveEndX - startX;
			if (X > 10) {
				_this.slideFuncPrev();
			} else if (X < -10) {
				_this.slideFuncNext();
			}
		})
	}
	window.Slide = Slide;
}(window))

/*===========================
Slide AMD Export
===========================*/
if (typeof(module) !== 'undefined') {
	module.exports = window.Slide;
} else if (typeof define === 'function' && define.amd) {
	define([], function() {
		'use strict';
		return window.Slide;
	});
}